/* watrayucproject.vala
 *
 * Copyright (C) 2009  Matias De la Puente
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.

 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.

 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
 *
 * Author:
 * 	Matias De la Puente <mfpuente.ar@gmail.com>
 */
using Watray;
using Gee;

public class WatrayUC.Project : Watray.Project
{
	private const string PROJECT_GROUP_NAME = "Watray uC Project";
	private KeyFile _key_file = new KeyFile ();
	private ArrayList<string> _extra_files = new ArrayList<string> ();
	private ArrayList<string> _build_files = new ArrayList<string> ();
	
	public string filename { set; get; }
	public string author { set; get; default="";}
	public string description { set; get; default=""; }
	public string ucontroller { set; get; default=""; }
	public string cflags { set; get; default=""; }
	public string asmflags { set; get; default=""; }
	public string ldflags { set; get; default=""; }
	
	public Gee.List<string> get_build_files ()
	{
		return new ReadOnlyList<string> (_build_files);
	}
	
	public void add_build_file (string file)
	{
		if (!_build_files.contains (file))
			_build_files.add (file);
	}
	
	public void remove_build_file (string file)
	{
		_build_files.remove (file);
	}
	
	public Gee.List<string> get_extra_files ()
	{
		return new ReadOnlyList<string> (_extra_files);
	}
	
	public void add_extra_file (string file)
	{
		if (!_extra_files.contains (file))
			_extra_files.add (file);
	}
	
	public void remove_extra_file (string file)
	{
		_extra_files.remove (file);
	}
	
	public Project (string name)
	{
		base (name);
	}
	
	public void open ()
	{
		_key_file.load_from_file (_filename, KeyFileFlags.NONE);
		this.name = _key_file.get_string (PROJECT_GROUP_NAME, "name");
		_description = _key_file.get_string (PROJECT_GROUP_NAME, "description");
		_author = _key_file.get_string (PROJECT_GROUP_NAME, "author");
		_ucontroller = _key_file.get_string (PROJECT_GROUP_NAME, "ucontroller");
		_cflags = _key_file.get_string (PROJECT_GROUP_NAME, "cflags");
		_asmflags = _key_file.get_string (PROJECT_GROUP_NAME, "asmflags");
		_ldflags = _key_file.get_string (PROJECT_GROUP_NAME, "ldflags");
		_build_files.clear ();
		foreach (string file in _key_file.get_string_list (PROJECT_GROUP_NAME, "build_files"))
			_build_files.add (file);
		_extra_files.clear ();
		foreach (string file in _key_file.get_string_list (PROJECT_GROUP_NAME, "extra_files"))
			_extra_files.add (file);
	}
	
	public void save ()
	{
		_key_file.set_string (PROJECT_GROUP_NAME, "name", this.name);
		_key_file.set_string (PROJECT_GROUP_NAME, "description", _description);
		_key_file.set_string (PROJECT_GROUP_NAME, "author", _author);
		_key_file.set_string (PROJECT_GROUP_NAME, "ucontroller", _ucontroller);
		_key_file.set_string (PROJECT_GROUP_NAME, "cflags", _cflags);
		_key_file.set_string (PROJECT_GROUP_NAME, "asmflags", _asmflags);
		_key_file.set_string (PROJECT_GROUP_NAME, "ldflags", _ldflags);
		var list = new string[_build_files.size];
		for (int i=0; i<_build_files.size; i++)
			list[i] = _build_files[i];
		_key_file.set_string_list (PROJECT_GROUP_NAME, "build_files", list);
		list = new string[_extra_files.size];
		for (int i=0; i<_extra_files.size; i++)
			list[i] = _extra_files[i];
		_key_file.set_string_list (PROJECT_GROUP_NAME, "extra_files", list);
		FileUtils.set_contents (_filename, _key_file.to_data (null));
	}
	
	public void refresh ()
	{
		foreach (string file in get_items ("/"))
		{
			if (!_build_files.contains (file) && !_extra_files.contains (file))
				remove_item (file);
		}
		foreach (string file in _build_files)
		{
			if (!item_exist("/" + file))
				create_item ("/" + file);
		}
		foreach (string file in _extra_files)
		{
			if (!item_exist ("/" + file))
				create_item ("/" + file);
		}
	}
}

